<template>
  <div id="detail">
    <!-- nav-bar -->
    <detail-nav-bar
      class="detail-nav"
      @title-click="titleClick"
      ref="nav"
    ></detail-nav-bar>
    <scroll
      class="content"
      ref="scroll"
      :probe-type="+3"
      @scroll="contentScroll"
    >
      <!-- detail-swiper -->
      <detail-swiper :topImages="topImages"></detail-swiper>
      <!-- detail-base-info -->
      <detail-base-info :goods="goods"></detail-base-info>
      <!-- detail-shop-info -->
      <detail-shop-info :shop="shop"></detail-shop-info>
      <!-- detail-goods-info -->
      <detail-goods-info
        :detail-info="detailInfo"
        @imgLoad="imgLoad"
      ></detail-goods-info>
      <!-- detail-param-info -->
      <detail-param-info
        ref="params"
        :param-info="paramInfo"
      ></detail-param-info>
      <!-- detail-rate -->
      <detail-rate ref="comment" :rate="rate"></detail-rate>
      <goods-list ref="recommend" :goods="recommends"></goods-list>
    </scroll>
    <detail-bottom-bar @add-cart="addToCart"></detail-bottom-bar>
    <back-top @click.native="backTop" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
import DetailNavBar from "./childComps/DetailNavBar";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo.vue";
import DetailRate from "./childComps/DetailRate.vue";
import DetailBottomBar from "./childComps/DetailBottomBar.vue";

import Scroll from "@/components/common/scroll/Scroll.vue";
import GoodsList from "../../components/content/goods/GoodsList.vue";

import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend,
} from "@/network/detail";

import { debounce } from "@/common/utils.js";
import { backTopMixin } from "@/common/mixin.js";

import { mapActions } from "vuex";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailRate,
    DetailBottomBar,
    Scroll,
    GoodsList,
  },
  mixins: [backTopMixin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      rate: [],
      recommends: [],
      themeTopYs: [],
      getThemeTopYs: null,
      currentIndex: 0,
      titleClickStatus: false,
      titleClickTimer: null,
    };
  },
  created() {
    // 保存传入的iid
    this.iid = this.$route.query.iid;
    // 根据iid请求详细信息
    getDetail(this.iid).then((res) => {
      // 获取顶部轮播信息
      const data = res.result;
      this.topImages = data.itemInfo.topImages;
      // 获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      // 创建店铺信息的对象
      this.shop = new Shop(data.shopInfo);
      // 保存商品详情信息
      this.detailInfo = data.detailInfo;
      // 获取参数信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );
      // 获取用户评价
      this.rate = data.rate.list || [];
    });
    // 请求推荐数据
    getRecommend().then((res) => {
      this.recommends = res.data.list;
    });
    // 给geThemeTopYsf赋值
    this.getThemeTopYs = debounce(() => {
      this.themeTopYs = [];
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.params.$el.offsetTop);
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      this.themeTopYs.push(Number.MAX_VALUE);
    }, 100);
  },
  methods: {
    ...mapActions(["addCart"]),
    imgLoad() {
      this.$refs.scroll.refresh();
      this.getThemeTopYs();
    },
    titleClick(index) {
      clearTimeout(this.titleClickTimer);
      this.titleClickStatus = true;
      this.currentIndex = index;
      this.$refs.nav.currentIndex = this.currentIndex;
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 500);
      this.titleClickTimer = setTimeout(() => {
        this.titleClickStatus = false;
      }, 500);
    },
    contentScroll(position) {
      if (this.titleClickStatus) return;
      const positionY = -position.y;
      for (let i = 0; i < this.themeTopYs.length - 1; i++) {
        if (i === this.currentIndex) continue;
        if (
          positionY >= this.themeTopYs[i] &&
          positionY < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
      // 是否显示回到顶部
      this.listenShowBackTop(position);
    },
    addToCart() {
      // 获取购物车需要展示的信息
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;
      this.addCart(product).then((res) => {
        this.$toast.show(res, 1500)
      });
    },
  },
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 25;
  background-color: #fff;
}
.detail-nav {
  position: relative;
  z-index: 25;
  background-color: #fff;
}
.content {
  height: calc(100vh - 44px - 49px);
}
</style>